<template>
	<view class="mySettle">
		<!-- 选择 -->
		<view class="settleLeft">
			<label class="radio" @click="allChecked">
				<radio color="#c00000" :checked="isFullCheck" />
				<text>全选</text>
			</label>
		</view>

		<!-- 总金额 -->
		<view class="total">
			<text>总结:</text>
			<text class="yuan">￥{{totalPrice}}</text>
		</view>

		<!-- 结算 -->
		<view class="settleSum" @click="settlement">
			结算（{{checkedCount}}）
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters,
		mapMutations
	} from 'vuex'

	export default {
		name: "my-settle",
		data() {
			return {
				second: 3,
				timer: null
			};
		},
		
		computed: {
			...mapGetters('moduleCart', ['checkedCount', 'total', 'totalPrice']),
			...mapState('m_address', ['token']),
			...mapState('moduleCart', ['cart']),
			...mapGetters('m_address', ['addrStr']),
			isFullCheck() {
				return this.checkedCount === this.total
			}
		},
		
		methods: {
			...mapMutations('moduleCart', ['updateAllChecked']),
			allChecked() {
				this.updateAllChecked(!this.isFullCheck)
			},
			
			settlement() {
				if (this.checkedCount === 0) return uni.$showmsg('请选择需要结算的物品')
				if (this.addrStr === '') return uni.$showmsg('请选择收货地址')
				if (this.token === '') return this.delayNavigate()
				this.patOrder()
			},
			
			async payOrder(){
				// 1.创建订单
				// 组织订单的信息对象
				const orderInfo={
					order_price:0.01,
					consignee_addr:this.addrStr,
					goods:this.cart.filter(x=>x.goods_state).map(x=>({
							goods_id:x.goods_id,
							goods_number:x.goods_count,
							goods_price:x.goods_price
						}))
				}
				// 发起请求创建订单
				const {data: res} = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
				if(res.meta.status !== 200) return uni.$showmsg('创建订单失败')
				// 得到服务器响应的 订单编号
				const orderNumber = res.message.order_number
				
				// 2.订单预支付
				// 发起请求获取订单的支付信息
				const {data: res2} = await uni.$http.post('/api/public/v1/my/orders/req.unifiedorder', {order_number:orderNumber})
				// 预支付订单生成失败
				if(res2.meta.status !== 200) return uni.$showmsg('预支付订单生成失败')
				// 得到订单支付相关的必要参数
				const payInfo = res2.message.pay
				
				// 3.发起微信支付
				// 调用uni.requestPayment() 发起微信支付
				const [err, res3] = await uni.requestPayment(payInfo)
				// 检测到订单未支付
				if(err) return uni.$showmsg('订单未支付！')
				// 完成支付后进一步查询支付结果
				const {data: res4} = await uni.$http.post('/api/public/v1/my/orders/chkorder', {order_number:orderNumber})
				// 检测到订单未成功
				if(res4.meta.status !== 200) return uni.$showmsg('订单未支付！')
				// 检测到订单成功
				uni.showToast({
					title:'订单支付成功！',
					icon:'success'
				})
			},
			
			delayNavigate() {
				this.second = 3
				this.showTips(this.second)

				this.timer = setInterval(() => {
					this.second--
					if (this.second <= 0) {
						clearInterval(this.timer)
						uni.switchTab({
							url: '/pages/profile/profile'
						})
						return
					}
					this.showTips(this.second)
				}, 1000)
			},
			
			showTips(n) {
				uni.showToast({
					icon: 'none',
					title: `请登录后再结算，将在${n}秒后跳到登录页面`,
					mask: true,
					duration: 1500
				})
			}
		}
	}
</script>

<style lang="scss">
	.mySettle {
		height: 100rpx;
		line-height: 100rpx;
		background-color: white;
		position: fixed;
		bottom: 0;
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-left: 10rpx;

		.radio {
			display: flex;
			align-items: center;
		}

		.total {
			.yuan {
				color: #c00000;
				font-weight: bold;
			}
		}

		.settleSum {
			background-color: #c00000;
			color: white;
			padding: 0 15rpx;
			text-align: center;
			width: 200rpx;
			height: 100rpx;
			line-height: 100rpx;
		}
	}
</style>
